<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'jsonTest.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <h3>JSON的使用(JSON)</h3>
    <input type="button" value="获取JSON数据" onclick="getInfo()" /><br/>
    <div id="showInfo"/></div>
    <script type="text/javascript">
    	var xmlhttp;
    	function createXMLHttpRequest(){  //定义一个变量xmlhttp
    		if(window.ActiveXObject){  ///如当前的浏览器为IE
    			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    		}else if(window.XMLHTTPRequest){//为其他浏览器
    		    xmlhttp = new XMLHTTPRequest();
    		}
    	}
    	
    	function getInfo(){
    		createXMLHttpRequest();
    		xmlhttp.open("GET","json/user.txt",true);
    		xmlhttp.onreadystatechange = returnInfo;
    		xmlhttp.send(null);
    	}
    	function returnInfo(){
    		if(xmlhttp.readyState == 4 ){
    			var info = xmlhttp.responseText;
    			//var func = new Function("return "+info);//转换成函数的方式
    			//var json =func();
    			var json =eval('('+info+')');//与上面的方式相同
    			var value="姓名："+json.name+"年龄："+json.age;
    			document.getElementById("showInfo").innerHTML = value;
    		}
    	}
    
    
    </script>
  </body>
</html>
